package com.atguigu.mvc.controller;

import com.atguigu.mvc.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;

/**
 * @author 陆小根
 * date: 2022/04/28
 * Description:
 * 测试获取请求参数的功能
 */
@Controller
public class ParamController {

  @RequestMapping("/testServletAPI")
  // 形参位置的request表示当前请求
  public String testServletAPI(HttpServletRequest request) {
    HttpSession session = request.getSession();
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    System.out.println("username = " + username + ",password = " + password);
    return "success";
  }


  @RequestMapping("/testParam")
  // 形参使用String：username = admin,password = 123,hobby = a,b,c
  // 形参使用String[]: username = admin,password = 123,hobby = [Ljava.lang.String;@7a82aebd 可以使用Arrays.toString
  public String testParam(
          @RequestParam(value = "user_name", required = false, defaultValue = "呵呵") String username,
          String password,
          String[] hobby,
          @RequestHeader(value = "Host", required = true, defaultValue = "sayHaHa") String host, // 获取请求头信息,
          @CookieValue("JSESSIONID") String JSESSIONID
          ) {
    System.out.println("username = " + username + ",password = " + password);
    System.out.println("host:" + host);
    System.out.println("爱好:" + Arrays.toString(hobby));
    System.out.println("JSESSIONID = " + JSESSIONID);
    // 若请求参数中出现多个同名的请求参数，可以在控制器方法的形参位置设置字符串或字符串数组接口此请求参数
    // 若使用字符串类型的形参，最终结果为请求参数的每一个值之间使用逗号进行拼接
    return "success";
  }

  @RequestMapping("/testPojo")
  public String testPojo(User user) { // 通过实体类获取参数
    System.out.println("user = " + user);
    return "success";
  }

}
